function renderCategory() {
  axios.get('/my/category/list').then(res => {
    let {status, data} = res.data;
    if( status ===0){
      let str =``;
      data.forEach( item => {
        str+=`
        <tr>
        <td>${item.name}</td>
        <td>${item.alias}</td>
        <td>
          <button  data-id="${item.id}" data-name="${item.name}" data-alias="${item.alias}" type="button" class="layui-btn layui-btn-xs">编辑</button>
          <button type="button" data-id="${item.id}"  class="del layui-btn layui-btn-xs layui-btn-danger">删除</button>
        </td>
      </tr>`
      });
      $('tbody').html(str);
    }
  })
}
renderCategory();

// 删除
$('tbody').on('click' ,'.del',function () {
  let id = $(this).data('id');
  layer.confirm('您确定要删除吗?', function(index){
    //do something
    axios.get(`/my/category/delete?id=${id}`).then( res =>{
      let {status, message} = res.data;
      if(status ===0){
        layer.msg(message);
        renderCategory();
      }
    })
    layer.close(index);
  });   
})

// 添加
let addIndex;
$('button:contains("添加类别")').on('click',function () {
 addIndex= layer.open({
   type:1,
   titel:'添加类别',
   content:$('#tpl-add').html(),
   area:['500px','250px']
 })
})

$('body').on('submit','#add-form',function (e) {
  e.preventDefault();
  let data= $(this).serialize();
  axios.post('/my/category/add',data).then( res=> {
  let {status, message}= res.data;
  if( status ===0){
    layer.msg(message);
    renderCategory();
    layer.close(addIndex);
  }
})
})


// 编辑
let  editIndex;
$('tbody').on('click','button:contains("编辑")',function () {
  let shuju =$(this).data();
  editIndex= layer.open({
    type:1,
    titel:'编辑分类',
    content:$('#tpl-edit').html(),
    area:['500px','250px'],
    success:function () {
      let form= layui.form;
      form.val('edit',shuju)
    }
  })
})

// 提交表单,完成修改
$('body').on('submit','#edit-form',function (e) {
  e.preventDefault();
  let data = $(this).serialize();
  axios.post('/my/category/update',data).then(res=>{
    let {status,message}=res.data;
    if(status===0){
      layer.msg(message);
      renderCategory();
      layer.close(editIndex)

    }
  })
})